package com.szewec.ps.ui.task;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.szewec.ps.R;
import com.szewec.ps.base.BaseActivity;
import com.szewec.ps.base.Constants;
import com.szewec.ps.databinding.ActivitySelectFacilityTypeBinding;
import com.szewec.ps.model.PresentValue;
import com.szewec.ps.model.response.ResponseData;
import com.szewec.ps.myview.TitleViewWithAdd;
import com.szewec.ps.myview.toast.bamtoast.BamToast;
import com.szewec.ps.ui.road.facilities.investigation.task.RoadInvestigationTaskActivity;
import com.szewec.ps.ui.road.facilities.intersection.task.RoadIntersectionTaskActivity;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.dialog.LoadingDialog;
import com.szewec.ps.util.okhttp.OkHttp;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;

public class SelectFacilityTypeActivity extends BaseActivity {
    private ActivitySelectFacilityTypeBinding binding;

    private String taskId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_select_facility_type);
        ButterKnife.bind(this);

        Bundle presentValue = getIntent().getExtras();
        if (presentValue != null) {
            PresentValue<Map<String, Object>> value = (PresentValue<Map<String, Object>>) presentValue.getSerializable(Constants.EXTRA_KEY.SELECT_FACILITY_TYPE);
            taskId = (String) value.getT().get("id");
        }
        System.out.println(taskId);
        initTitleBar();
        initView();
        initData();
    }

    private void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", taskId);
        OkHttp.get("/tpms-plan-service/api/road-facilities-task/", map, LoadingDialog.getInstance(this), result -> {
            ResponseData res = JSON.parseObject(result, ResponseData.class);
            if (res.getCode() == 10000) {
                binding.tvaAddCrossingFacilities.setRightType(TitleViewWithAdd.EDIT);
                binding.tvaAddCrossingFacilities.setIronClickListener(v -> {
                    //路口设施类型编辑事件
                    Intent intent = new Intent(this, RoadIntersectionTaskActivity.class);
                    PresentValue<String> presentValue = new PresentValue<>(Constants.PRESENT_VALUE_FLAG.EDIT, taskId);
                    intent.putExtra(Constants.EXTRA_KEY.ROAD_INTERSECTION_TASK, presentValue);
                    startActivity(intent);
                });
//                BamToast.showText(this, test.getMsg(), true);
            } else {
                BamToast.showText(this, res.getMsg(), false);
            }
        });
    }

    //加载标题
    private void initTitleBar() {
        binding.titleBar.setLeftLayoutVisibility(View.VISIBLE);
        binding.titleBar.setLeftLayoutClickListener(this::finish);
        binding.titleBar.setRightLayoutVisibility(View.VISIBLE);
    }

    private void initView() {
        if (StringUtils.isEmpty(taskId)) {
            BamToast.showText(this, "数据有误，请联系管理员", false);
            return;
        }
        //路段设施类型添加事件
        binding.tvaAddRoadFacilities.setIronClickListener(v -> {
            Intent intent = new Intent(this, RoadInvestigationTaskActivity.class);
            PresentValue<String> presentValue = new PresentValue<>(Constants.PRESENT_VALUE_FLAG.ADD, taskId);
            intent.putExtra(Constants.EXTRA_KEY.ROAD_INVESTIGATION_TASK, presentValue);
            startActivity(intent);
        });

        //路口设施类型添加事件
        binding.tvaAddCrossingFacilities.setIronClickListener(v -> {
            Intent intent = new Intent(this, RoadIntersectionTaskActivity.class);
            PresentValue<String> presentValue = new PresentValue<>(Constants.PRESENT_VALUE_FLAG.ADD, taskId);
            intent.putExtra(Constants.EXTRA_KEY.ROAD_INTERSECTION_TASK, presentValue);
            startActivity(intent);
        });
    }
}